package com.lzh.controller;

import com.lzh.bean.Dict;
import com.lzh.common.bean.ResultBean;
import com.lzh.common.controller.BaseController;
import com.lzh.common.group.V;
import com.lzh.service.DictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api")
@Api(tags = "字典管理")
public class DictController extends BaseController {

    @Autowired
    DictService dictService;

    /**
     * 保存字典
     * @param dict   传入的字典信息
     * @param bindingResult  验证结果
     * @return
     */
    @ApiOperation("保存字典")
    @PostMapping("/dict")
    public ResultBean saveDict(@Validated @RequestBody Dict dict, BindingResult bindingResult) {
        //验证结果
        V.validate(bindingResult);
        int save = dictService.save(dict);
        String tmp = save > 0 ? "保存成功":"保存失败";
        return success("保存字典",tmp);
    }

    /**
     *
     * @param pid
     * @return
     */
    @ApiOperation("查询字典")
    @GetMapping("/dict/child/{pid}")
    public ResultBean<List<Dict>> queryDict(@PathVariable("pid") int pid) {

        List query = dictService.query(pid);
        return success("查询字典",query);
    }

    /**
     * 更新字典
     * @param dict
     * @param bindingResult
     * @return
     */
    @ApiOperation("更新字典")
    @PutMapping("/dict")
    public ResultBean updateDict(@Validated @RequestBody Dict dict, BindingResult bindingResult) {
        V.validate(bindingResult);
        int update = dictService.update(dict);
        String tmp = update > 0 ? "更新成功":"更新失败";
        return success("更新字典",tmp);
    }


    /**
     * 删除字典
     * @param id
     * @return
     */
    @ApiOperation("删除字典")
    @DeleteMapping("/dict/{id}")
    public ResultBean deleteDict(@PathVariable("id") int id) {
        int delete = dictService.delete(id);
        String tmp = delete > 0 ? "删除成功":"删除失败";
        return success("删除字典",tmp);
    }

}
